package com.cxyd.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxyd.common.page.PageData;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.modules.sys.dto.SysUserDTO;
import com.cxyd.modules.sys.service.SysUserService;
import com.cxyd.modules.wms.dao.StationDao;
import com.cxyd.modules.wms.dto.StationDTO;
import com.cxyd.modules.wms.entity.StationEntity;
import com.cxyd.modules.wms.service.StationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 站台信息
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@Service
public class StationServiceImpl extends CrudServiceImpl<StationDao, StationEntity, StationDTO> implements StationService {
    private final SysUserService userService;

    public StationServiceImpl(SysUserService userService) {
        this.userService = userService;
    }

    @Override
    public PageData<StationDTO> page(Map<String, Object> params) {
        //1.查询站台信息
        final IPage<StationEntity> page = baseDao.selectPage(
                getPage(params, "station_id", true),
                getWrapper(params)
        );
        //2.获取用户信息
        final List<SysUserDTO> userList = userService.getList();
        //3.将creator与updater的username通过id进行绑定
        page.getRecords().forEach(entity -> {
            if (entity.getCreator() != null) {
                entity.setCreatorName(userService.getUsernameById(entity.getCreator(), userList));
            }
            if (entity.getUpdater() != null) {
                entity.setUpdaterName(userService.getUsernameById(entity.getUpdater(), userList));
            }
        });
        return getPageData(page, StationDTO.class);
    }

    @Override
    public QueryWrapper<StationEntity> getWrapper(Map<String, Object> params) {
        String stationNo = (String) params.get("stationNo");
        String stationType = (String) params.get("stationType");

        QueryWrapper<StationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(stationNo), "station_no", stationNo);
        wrapper.eq(StringUtils.isNotBlank(stationType), "station_type", stationType);

        return wrapper;
    }


}